package com.app.micaihu.view.user.userinfo.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;

import com.android.volley.VolleyError;
import com.app.micaihu.R;
import com.app.micaihu.app.AppApplication;
import com.app.micaihu.base.BaseOldActivity;
import com.app.micaihu.base.bean.DataBean;
import com.app.micaihu.bean.infor.AvatarBean;
import com.app.micaihu.bean.infor.UserInfor;
import com.app.micaihu.configure.URIContant;
import com.app.micaihu.custom.components.ParamMap;
import com.app.widget.LoadView;
import com.app.micaihu.listener.HttpListener;
import com.app.micaihu.manager.UserInforManager;
import com.app.micaihu.utils.DialogUtils;
import com.app.micaihu.utils.HttpUtils;
import com.app.micaihu.view.user.userinfo.adapter.AvatarAdapter;
import com.app.utils.util.ToastUtils;
import com.google.gson.reflect.TypeToken;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class AvatarActivity extends BaseOldActivity implements View.OnClickListener {

    private GridView gridView;
    private LoadView mLoadView;
    private AvatarAdapter mierAvatarDapter;
    private AvatarBean his_mierAvatarBean, mierAvatarBean;
    private ImageView his_select;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addContentView(R.layout.activity_huge_avater);
        initViews();
    }

    protected void initViews() {
        setTitle("虎哥头像");
        setTitleRightText("完成").setOnClickListener(this);
        gridView = (GridView) findViewById(R.id.mier_avatar_gridview);
        mLoadView = (LoadView) findViewById(R.id.mier_avatar_loadview);
        mLoadView.setErrorPageClickListener(this);
        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                mierAvatarBean = (AvatarBean) parent.getItemAtPosition(position);

                if (mierAvatarBean == null) {
                    return;
                }
                ImageView select_iv = (ImageView) view.findViewById(R.id.mieravatar_item_select);
                //
                if (his_mierAvatarBean != null && !mierAvatarBean.equals(his_mierAvatarBean)) {
                    his_mierAvatarBean.selected = false;
                }

                if (select_iv.getVisibility() == View.VISIBLE) {
                    select_iv.setVisibility(View.GONE);
                    mierAvatarBean.selected = false;
                } else {
                    select_iv.setVisibility(View.VISIBLE);
                    mierAvatarBean.selected = true;
                }
                if (his_select != null && !his_select.equals(select_iv))
                    his_select.setVisibility(View.GONE);

                his_select = select_iv;
                his_mierAvatarBean = mierAvatarBean;

                if (mierAvatarDapter != null)
                    mierAvatarDapter.notifyDataSetChanged();

            }
        });
        loadData();
    }


    /**
     * 加载数据
     */
    private void loadData() {
        Map<String, String> requestParams = new HashMap<>();
        HttpListener<DataBean<List<AvatarBean>>> httpListener = new HttpListener<DataBean<List<AvatarBean>>>() {

            @Override
            public void onStart() {
                if (mLoadView != null) {
                    mLoadView.showLoadPage("");
                }
            }

            @Override
            public void onError(VolleyError error) {
                if (mLoadView != null) {
                    mLoadView.showErrorPage(AppApplication.getInstance().getString(R.string.neterror_click));
                }
            }

            @Override
            public void onSuccess(DataBean<List<AvatarBean>> response) {
                if (response.noErrorData() && !isFinishing()) {

                    if (mierAvatarDapter == null) {
                        mierAvatarDapter = new AvatarAdapter(response.getData(), mActivity);
                        gridView.setAdapter(mierAvatarDapter);
                    } else {
                        mierAvatarDapter.resetList(response.getData());
                        mierAvatarDapter.notifyDataSetChanged();
                    }
                    if (gridView.getVisibility() == View.GONE) {
                        gridView.setVisibility(View.VISIBLE);
                    }
                    mLoadView.showSuccess();
                } else {
                    mLoadView.showErrorPage(response.getNnderstoodMsg());
                }
            }
        };
        //请求网络
        addPostCacheRequest(URIContant.AVATER_LIST, new TypeToken<DataBean<List<AvatarBean>>>() {
        }.getType(), requestParams, httpListener);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.page_head_function_text:
                if (mierAvatarBean != null) {
                    changeAvatar();
                } else {
                    ToastUtils.makeText("请选择头像");
                }
                break;
            case R.id.error_page:
                loadData();
                break;
        }
    }

    /**
     * 更改用户信息
     */
    private void changeAvatar() {
        if (!UserInforManager.getInstance().isLogin() || mierAvatarBean == null) {
            DialogUtils.getInstance().dismissProgressDialog();
            return;
        }
        ParamMap params = new ParamMap();
        params.putEncode("uid", UserInforManager.getInstance().getUserInfor().getUid());
        params.putEncode("headId", mierAvatarBean.headId);
        HttpUtils.ParmsMapMD5(params);
        HttpListener<DataBean<AvatarBean>> httpListener = new HttpListener<DataBean<AvatarBean>>() {

            @Override
            public void onStart() {
                DialogUtils.getInstance().showProgressDialog(AvatarActivity.this, "正在修改");
            }

            @Override
            public void onError(VolleyError error) {
                DialogUtils.getInstance().dismissProgressDialog();
                ToastUtils.makeText("修改失败！");
            }

            @Override
            public void onSuccess(DataBean<AvatarBean> response) {
                DialogUtils.getInstance().dismissProgressDialog();
                if (response.noError() && !isFinishing()) {
                    ToastUtils.makeText("修改成功！");
                    UserInfor userInfor = UserInforManager.getInstance().getUserInfor();
                    if (userInfor != null) {
                        userInfor.setHeadPic(mierAvatarBean.url);
                        UserInforManager.getInstance().saveUserInfor(userInfor);
                        onBackPressed();
                    }
                } else {
                    ToastUtils.makeText("修改失败！");
                }
            }
        };
        //请求网络
        addPostCacheRequest(URIContant.AVATER_LIST_UP, new TypeToken<DataBean<AvatarBean>>() {
        }.getType(), params, httpListener);


    }
}
